﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DecoderApp
{
    public partial class Form1 : Form
    {
        BackgroundWorker _bw = new BackgroundWorker();


        string status = "8e 02 33 00 0d 8d ad 00 00 01 00 01 04 57 34 00 00 03 04 08 8a 29 00 04 08 58 d4 23 9d 96 db 04 00 05 02 5c 00 06 02 80 00 08 02 00 00 81 04 be 13 04 00 8f";
        byte[] statusBytes = new byte[] { 0x8e, 0x02, 0x1f, 0x00, 0x76, 0xc4, 0x00, 0x00, 0x02, 0x00, 0x01, 0x02, 0x2b, 0x00, 0x07, 0x02, 0x20, 0x00, 0x0c, 0x01, 0x77, 0x06, 0x01, 0x00, 0x81, 0x04, 0xbe, 0x13, 0x04, 0x00, 0x8f };
        byte[] passingStartBytes = new byte[] { 0x8e, 0x02, 0x33, 0x00, 0xeb, 0x1d, 0x00, 0x00, 0x01, 0x00, 0x01, 0x04, 0x9d, 0x09, 0x00, 0x00, 0x03, 0x04, 0xe4, 0xd2, 0x36, 0x00, 0x04, 0x08 };
        byte[] passingEndBytes = new byte[]{0x05, 0x02, 0x5f, 0x00, 0x06, 0x02, 0x2e, 0x00, 0x08, 0x02, 0x00, 0x00, 0x81, 0x04, 0xbe, 0x13, 0x04, 0x00, 0x8f};
        public Form1()
        {
            InitializeComponent();

            _bw.DoWork += _bw_DoWork;

            _bw.RunWorkerAsync();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void _bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            while (true)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    try
                    {
                        // Create a TcpClient.
                        // Note, for this client to work you need to have a TcpServer
                        // connected to the same address as specified by the server, port
                        // combination

                        TcpListener server = new TcpListener(IPAddress.Any, 5403);

                        server.Start();

                        try
                        {
                            TcpClient client = server.AcceptTcpClient(); // Get client connection

                            NetworkStream netStream = client.GetStream();

                            while (client.Connected)
                            {
                                netStream.Write(statusBytes, 0, statusBytes.Length);
                                netStream.Flush();

                                Thread.Sleep(5000);


                                DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                                //return epoch.AddMilliseconds(unixTime / 1000);

                                TimeSpan epoch = DateTime.Now.Subtract(start);

                                byte[] time = BitConverter.GetBytes(epoch.Milliseconds/1000);

                                byte[] passingBytes = new byte[passingStartBytes.Length + time.Length + passingEndBytes.Length];
                                System.Buffer.BlockCopy(passingStartBytes, 0, passingBytes, 0, passingStartBytes.Length);
                                System.Buffer.BlockCopy(time, 0, passingBytes, passingStartBytes.Length, time.Length);
                                System.Buffer.BlockCopy(passingEndBytes, 0, passingBytes, passingStartBytes.Length + time.Length, passingEndBytes.Length);

                                netStream.Write(passingBytes, 0, passingBytes.Length);
                                netStream.Flush();

                                Thread.Sleep(5000);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
    }
}
